package com.silence.gankio.mvvm.view

import android.annotation.SuppressLint
import android.arch.lifecycle.Observer
import android.graphics.Bitmap
import android.os.Bundle
import android.support.v7.graphics.Palette
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import com.jakewharton.rxbinding2.view.RxView
import com.silence.gankio.R
import com.silence.gankio.config.Constance
import com.silence.gankio.mvvm.BaseActivity
import com.silence.gankio.mvvm.viewModel.SplashViewModel
import com.silence.gankio.sp.SplashSP
import com.silence.gankio.util.StatusBarUtils
import com.xuexiang.rxutil2.rxjava.RxJavaUtils
import com.xuexiang.xui.utils.SnackbarUtils
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_splash.*
import java.util.concurrent.TimeUnit

class SplashActivity : BaseActivity<SplashViewModel>() {

    override fun getModelClass(): Class<SplashViewModel> = SplashViewModel::class.java

    override fun getActivityContent() = R.layout.activity_splash

    //    private lateinit var job: Job
    private lateinit var job: Disposable

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        initView()
        initListener()
        initCountdown()
    }

    private fun initView() {
        hideToolBar()
        Glide.with(this)
            .load(SplashSP.INSTANCE.getImageUrl())
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
                    resource?.let { bitmap ->
                        iv_splash.setImageBitmap(bitmap)

                        Palette.from(bitmap).generate {
                            val color = it?.getVibrantColor(resources.getColor(R.color.colorPrimary))
                                ?: resources.getColor(R.color.colorPrimary)
                            StatusBarUtils.setStatusBarColor(
                                this@SplashActivity,
                                color, false
                            )
                            iv_splash.setBackgroundColor(color)
                        }
                    }
                }
            })
        mBaseModel?.loadImageUrl()
    }


    @SuppressLint("CheckResult")
    private fun initListener() {
        mBaseModel?.getImageUrl()?.observe(this, Observer<String> { t ->
            SnackbarUtils.Short(content_view, "已获取最新图片")
            Glide.with(this@SplashActivity)
                .load(t)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
                        resource?.let { bitmap ->
                            iv_splash.setImageBitmap(bitmap)

                            Palette.from(bitmap).generate {
                                val color = it?.getVibrantColor(resources.getColor(R.color.colorPrimary))
                                    ?: resources.getColor(R.color.colorPrimary)
                                StatusBarUtils.setStatusBarColor(
                                    this@SplashActivity,
                                    color, false
                                )
                                iv_splash.setBackgroundColor(color)
                            }
                        }
                    }
                })
        })

        RxView.clicks(tv_countdown)
            .throttleFirst(2, TimeUnit.SECONDS)
            .subscribe {
                job.dispose()
                getRouter(Constance.ACTIVITY_URL_MAIN_BINGING)
                    .navigation()
                finish()
            }
    }

    private fun initCountdown() {
        job = RxJavaUtils.countDown(5)
            .subscribe {
                tv_countdown.text = it.toString()
                if (0L == it) {
                    getRouter(Constance.ACTIVITY_URL_MAIN_BINGING)
                        .navigation()
                    finish()
                }
            }
//        val count = 5
//        job = GlobalScope.launch {
//            for (i in count.downTo(1)) {
//                delay(1000)
//                tv_countdown.text = i.toString()
//            }
//            getRouter(Constance.ACTIVITY_URL_MAIN_BINGING)
//                        .navigation()
//                    finish()
//        }
    }
}
